package com.wuyou.sso.common.utils;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * MD5
 *
 * @Date 2025/4/11 16:05
 * @Author 无忧
 */
public class MD5 {
    private static final Logger log = LoggerFactory.getLogger(MD5.class);
    private static final String[] HEX_DIGITS = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public MD5() {
    }

    public static String md5(String str) {
        str = null == str ? "" : str;

        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(str.getBytes(StandardCharsets.UTF_8));
            StringBuffer sb = new StringBuffer();

            for (int i = 0; i < bytes.length; ++i) {
                sb.append(byteToHexString(bytes[i]));
            }

            return sb.toString().toLowerCase();
        } catch (Exception var5) {
            if (log.isInfoEnabled()) {
                log.info("There was a problem encrypting the string {} using md5, and the reason for the problem is {}", str, var5);
            }

            return null;
        }
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (b < 0) {
            n = b + 256;
        }

        int d1 = n / 16;
        int d2 = n % 16;
        String var10000 = HEX_DIGITS[d1];
        return var10000 + HEX_DIGITS[d2];
    }

    public static String md5Short(String str) {
        return StringUtils.substring(md5(str), 8, 24);
    }

    public static synchronized String md5(File file) {
        FileInputStream inputStream = null;

        Object var3;
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            inputStream = new FileInputStream(file);
            byte[] buffer = new byte[1024];

            int length;
            while ((length = inputStream.read(buffer)) != -1) {
                md5.update(buffer, 0, length);
            }

            StringBuilder sb = new StringBuilder();
            byte[] var6 = md5.digest();
            int var7 = var6.length;

            for (int var8 = 0; var8 < var7; ++var8) {
                byte b = var6[var8];
                sb.append(String.format("%02x", b));
            }

            String var16 = sb.toString().toLowerCase();
            return var16;
        } catch (Exception var13) {
            if (log.isInfoEnabled()) {
                log.info("There was a problem calculating the md5 value of file {}, the problem is {}", file, var13.getMessage());
            }

            var3 = null;
        } finally {
            CloseUtil.close(new Closeable[]{inputStream});
        }

        return (String) var3;
    }

    public static String md5Short(File file) {
        return StringUtils.substring(md5(file), 8, 24);
    }
}
